package com.eshop.eshop2021_v1.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eshop.eshop2021_v1.mapper.before.GoodTypeMapper;
import com.eshop.eshop2021_v1.mapper.before.GoodsMapper;
import com.eshop.eshop2021_v1.pojo.GoodType;
import com.eshop.eshop2021_v1.pojo.Goods;
import com.eshop.eshop2021_v1.pojo.User;
import com.eshop.eshop2021_v1.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: 若若叶
 * @date: 2021/10/7 9:40 FileName: GoodsServiceImpl Description:
 */
@Service
public class GoodsServiceImpl implements GoodsService {
  @Autowired GoodsMapper goodsMapper;
  @Autowired GoodTypeMapper goodTypeMapper;

  @Override
  public String showGoods(Model model) {
    // 查询广告商品
    QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
    QueryWrapper<Goods> advertisement = queryWrapper.eq("isAdvertisement", 1);
    List<Goods> advertisementGoods = goodsMapper.selectList(advertisement);
    model.addAttribute("advertisementGoods", advertisementGoods);
    // 查询商品类型
    QueryWrapper<GoodType> queryWrapper1 = new QueryWrapper<>();
    List<GoodType> goodTypes = goodTypeMapper.selectList(queryWrapper1);
    model.addAttribute("goodTypes", goodTypes);

    // 查询推荐商品
    List<Goods> allRecommendGoods = goodsMapper.selectAllRecommendGoods();
    model.addAttribute("allRecommendGoods", allRecommendGoods);
    // 查询最新商品
    List<Goods> allNewGoods = goodsMapper.selectAllNewGoods();
    model.addAttribute("allNewGoods", allNewGoods);
    return "user/index";
  }

  @Override
  public List<Goods> findGoodsByTypeId(Integer typeId, HttpSession session) {
    if (null == typeId) {
      throw new RuntimeException("商品可能下架");
    }
    User user = (User) session.getAttribute("loginUser");
    if (null == user) {
      throw new RuntimeException("登录超时");
    }
    return goodTypeMapper.findGoodsByTypeId(typeId);
  }

  @Override
  public List<Goods> showadvertisementGoods() {
    // 查询广告商品
    QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
    QueryWrapper<Goods> advertisement = queryWrapper.eq("isAdvertisement", 1);
    List<Goods> advertisementGoods = goodsMapper.selectList(advertisement);
    return advertisementGoods;
  }

  @Override
  public List<GoodType> showGoodsType() {
    // 查询商品类型
    QueryWrapper<GoodType> queryWrapper1 = new QueryWrapper<>();
    List<GoodType> goodTypes = goodTypeMapper.selectList(queryWrapper1);
    return goodTypes;
  }

  @Override
  public String findGoodByGoodId(Integer goodId, Model model) {
    if (null == goodId) {
      throw new RuntimeException("商品下架");
    }
    Goods goods = goodsMapper.findGoodByGoodId(goodId);

    // Integer goodsTypeId = goods.getGoodsTypeId();
    // GoodType goodType = goodTypeMapper.selectById(goodsTypeId);
    // model.addAttribute("goodType", goodType);

    model.addAttribute("goods", goods);

    // 获取广告，最新商品
    List<Goods> list = showadvertisementGoods();
    List<GoodType> goodTypes = showGoodsType();
    model.addAttribute("advertisementGoods", list);
    model.addAttribute("goodTypes", goodTypes);
    return "user/goodsDetail";
  }
}
